﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace ERP.Manager.DataHelper
{
    public class SQLHelper
    {
        private static readonly string connectString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        
        
        public static void PrepareCommand(SqlCommand cmd, SqlConnection con, CommandType cmdType, string cmdText, SqlParameter[] parames)
        {
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }
            cmd.Connection = con;
            cmd.CommandType = cmdType;
            cmd.CommandText = cmdText;

            if (parames != null)
            {
                foreach (SqlParameter par in parames)
                {
                    cmd.Parameters.Add(par);
                }
            }
        }
        /// <summary>
        /// 进行查询的操作；
        /// </summary>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="parames"></param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReader(CommandType cmdType, string cmdText, params SqlParameter[] parames)
        {
            SqlCommand command = new SqlCommand();
            SqlConnection connection = new SqlConnection(connectString);
            try
            {
                PrepareCommand(command, connection, cmdType, cmdText, parames);
                SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                command.Parameters.Clear();
                return reader;
            }
            catch (Exception)
            {
                connection.Close();
                connection.Dispose();
                throw;
            }
        }

        /// <summary>
        /// 进行增、删、改的操作
        /// </summary>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="parames"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(CommandType cmdType, string cmdText, params SqlParameter[] parames)
        {
            SqlCommand command = new SqlCommand();
            SqlConnection connection = new SqlConnection(connectString);
            try
            {
                PrepareCommand(command, connection, cmdType, cmdText, parames);
                int result = command.ExecuteNonQuery();
                command.Parameters.Clear();
                return result;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                connection.Close();
                connection.Dispose();
            }
        }
        /// <summary>
        /// 查询记录总的条数
        /// </summary>
        /// <param name="cmdType"></param>
        /// <param name="cmdText"></param>
        /// <param name="parames"></param>
        /// <returns></returns>
        public static object ExecuteScalar(CommandType cmdType, string cmdText, params SqlParameter[] parames)
        {
            SqlConnection conn = new SqlConnection(connectString);
            SqlCommand cmd = new SqlCommand();

            PrepareCommand(cmd, conn, cmdType, cmdText, parames);
            object re = cmd.ExecuteScalar();
            cmd.Parameters.Clear();
            return re;
        }
    }
}
